package com.cgc.game.base;

public class MessagePacker {

	MessageQueue msgVector[] = new MessageQueue[Message.MSG_SIZE];
	
	private static MessagePacker instance;
	
	public static MessagePacker getInstance(){
		if(null == instance){
			instance = new MessagePacker();
		}
		return instance;
	}
	
	private MessagePacker(){
		int i = 0;
		for(i = 0; i < Message.MSG_SIZE; i++){
			msgVector[i] = new MessageQueue(10);
		}
	}
	
	public Message getMessage(byte msgType) throws Exception{
		if(msgType < 0 || msgType >= Message.MSG_SIZE){
			throw new Exception("error param 1 in getMessage, too small or too large");
		}
		
		return msgVector[msgType].get();
	}
	
	public void postMessage(Message msg){
		if(null != msg && msg.mParam1 >= 0 && msg.mParam1 < Message.MSG_SIZE){
			msgVector[msg.mParam1].put(msg);
		}
	}
	
	public void postMessage(byte cbParam1, byte cbParam2, Object msgBody) {
		if(cbParam1 < 0 || cbParam1 >= Message.MSG_SIZE){
			System.out.println("Msg:invalid cbParam1");
		}
		
		msgVector[cbParam1].put(cbParam1, cbParam2, msgBody);
	}
	
}
